package android.bluebox.model;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;

public class GPSDetector extends Activity {

	double lattitude = -1;
	double longitude = -1;
	
	LocationManager mlocManager;
	LocationListener mlocListener;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		mlocListener = new MyLocationListener();
		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
		mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
	}
	
	public double getLattitude() {
		return lattitude;
	}
	
	public double getLongitude() {
		return longitude;
	}
	
	public void removeUpdates() {
		mlocManager.removeUpdates(mlocListener);
	}
	
	public void update() {
		mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener);
		mlocManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, mlocListener);
	}

	public class MyLocationListener implements LocationListener {
		public void onLocationChanged(Location loc) {
			lattitude = loc.getLatitude();
			longitude = loc.getLongitude();
			String Text = "My current location is: " +
			"Latitude = " + loc.getLatitude() +
			"Longitude = " + loc.getLongitude();
			Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show();
			removeUpdates();
		}

		@Override
		public void onProviderDisabled(String provider) {
			Toast.makeText(getApplicationContext(), "Gps Disable", Toast.LENGTH_SHORT).show();
			
		}

		@Override
		public void onProviderEnabled(String provider) {
			Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
	}
}
